package com.yonyou.findata.kafka.newapi;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.clients.consumer.OffsetCommitCallback;
import org.apache.kafka.common.TopicPartition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 手工提交offset
 *  1. commitSync (同步提交)
 *  2. commitAsync (异步提交)
 *
 *  两者的相同点是，都会将本次 poll 的一批数据最高的偏移量提交;
 *  不同点是， commitSync 阻塞当前线程，一直到提交成功，并且会自动失败重试(由不可控因素导致， 也会出现提交失败);
 *  而 commitAsync 则没有失败重试机制，故有可能提交失败
 *
 * 异步提交的方式可以提高吞吐量,故应该选择这种方式
 *
 * @author pizhihui
 * @date 2017-11-08
 */
public class ConsumerMannualAsyncCommit extends BaseConsumer{

    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerMannualAsyncCommit.class);

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("group.id", "test");
        // 关闭自动提交offset
        props.put("enable.auto.commit", "false");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.IntegerDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList("foo", "bar"));

        final int minBatchSize = 200;

        List<ConsumerRecord<String, String>> buffer = new ArrayList<>();
        try {
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(110);
                for (ConsumerRecord<String, String> record : records) {
                    buffer.add(record);
                }
                if (buffer.size() >= minBatchSize) {
                    handle(buffer);
                    // 表示收到所有的消息为"已提交"
                    consumer.commitAsync(new OffsetCommitCallback() {
                        @Override
                        public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception exception) {
                            if (null != exception) {
                                System.out.println("提交offset异常,需要进行一定的处理");
                            }
                        }
                    });
                    buffer.clear();
                }
            }
        } finally {
            consumer.close();
        }
    }

}
